package java8;

import org.junit.Test;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/7/21 15:48
 * 重复注解与类型注解
 */
public class TestAnnotation {
    @MyAnnotation("hello")
    @MyAnnotation("world")
    private void show(@MyAnnotation("abc") String str) {
    }

    @Test
    public void test() {
        Class<TestAnnotation> cls = TestAnnotation.class;
        try {
            Method method = cls.getDeclaredMethod("show", String.class);
            MyAnnotation[] anns = method.getAnnotationsByType(MyAnnotation.class);
            for (MyAnnotation ann : anns) {
                System.out.println(ann.value());
            }

            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                MyAnnotation[] pannos = parameter.getAnnotationsByType(MyAnnotation.class);
                Arrays.stream(pannos).forEach((a) -> {
                    System.out.println("Parameter value:" + (a.value()));
                });
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
